package com.tmsps.ne4weixin.api;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.tmsps.ne4weixin.bean.BaseResultBean;
import com.tmsps.ne4weixin.config.WxConfig;
import com.tmsps.ne4weixin.utils.WXHttpUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @Title: MenuConditionalAPI.java
 * @Package: com.tmsps.ne4weixin.api
 * @Description: 个性化菜单管理API
 */
@Slf4j
public class MenuConditionalAPI extends BaseAPI {
	
	private String accessToken = null;
	
	public MenuConditionalAPI(WxConfig config) {
		super(config);
		this.accessToken = config.getAccessToken();
	}
	
	public MenuConditionalAPI(String accessToken) {
		super(null);
		this.accessToken = accessToken;
	}
	
	/**
	 * 创建个性化菜单
	 * @param menuJSON
	 * @return
	 */
	public BaseResultBean createMenu(String menuJSON) {
		String url = String.format(ADD_CONDITIONAL,accessToken);
		String result = WXHttpUtil.postJson(url, menuJSON);
		log.info("创建个性化菜单返回信息:{}",result);
		return JSONObject.parseObject(result, BaseResultBean.class);
	}

	/**
	 * 测试个性化菜单匹配结果
	 * @param openid
	 * @return
	 */
	public BaseResultBean tryMatch(String openid) {
		String url = String.format(TRY_MATCH,accessToken);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("user_id", openid);
		String result = WXHttpUtil.postJson(url, JSONObject.toJSONString(paramMap));
		log.info("测试个性化菜单匹配结果返回信息:{}",result);
		return JSONObject.parseObject(result, BaseResultBean.class);
	}
	
	/**
	 * 查询个性化菜单 注： 普通自定义菜单查询接口可以获取默认菜单和全部个性化菜单信息
	 * @return
	 */
	public String getMenu() {
		String url = String.format(GET_MENU, accessToken);
		return WXHttpUtil.get(url);
	}
	
	/**
	 * 删除个性化菜单
	 * @param menuJSON
	 * @return
	 */
	public BaseResultBean deleteMenu(String menuid) {
		String url = String.format(DEL_CONDITIONAL,accessToken);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("menuid", menuid);
		String result = WXHttpUtil.postJson(url, JSONObject.toJSONString(paramMap));
		log.info("删除个性化菜单返回信息:{}",result);
		return JSONObject.parseObject(result, BaseResultBean.class);
	}
}
